package dm.bsi;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Iterator;

import org.dom4j.DocumentException;

import indi.zjh.dp.db.Session;
import indi.zjh.dp.error.UnsuportedFieldException;
import indi.zjh.dp.orm.Business;
import indi.zjh.dp.orm.Query;
import indi.zjh.dp.orm.Relation;
import indi.zjh.dp.orm.Result;
import indi.zjh.dp.type.Text;

public class Log extends Business {
	public static final int STATUS_SUCCESS = 0;
	public static final int STATUS_FAILURE = 1;
	public static final int STATUS_INFO = 9999;
	protected Text content = new Text("nil");
	protected int status = 0;
	protected Relation logNode = new Relation(LogNode.class);
	
	public static String toDictString(Method m)
	{
		if(m.getName().equals("success"))
		{
			return "记录成功日志";
		}
		else if(m.getName().equals("failure"))
		{
			return "记录失败日志";
		}
		else if(m.getName().equals("info"))
		{
			return "记录信息日志";
		}
		else if(m.getName().equals("printLog"))
		{
			return "打印日志";
		}
		return Business.toDictString(m);
	}
	
	public static String toDictString(Field f)
	{
		if(f.getName().equals("content"))
		{
			return "日志内容";
		}
		else if(f.getName().equals("status"))
		{
			return "日志结果状态, 0:成功, 1:失败, 2:超时";
		}
		else if(f.getName().equals("logNode"))
		{
			return "日志关联的对象";
		}
		return Business.toDictString(f);
	}
	
	private static Log logSingleNode(LogNode node, String content, int status) throws ClassNotFoundException, UnsuportedFieldException, DocumentException, Exception
	{
		Log log = new Log();
		log.setContent(content);
		log.setStatus(status);
		log.getLogNode().add(node);
		return log;
	}
	
	private static Log logMultiNodes(LogNode[] nodes, String content, int status) throws ClassNotFoundException, UnsuportedFieldException, DocumentException, Exception
	{
		Log log = new Log();
		log.setContent(content);
		log.setStatus(status);
		for(int i = 0; i < nodes.length; ++i)
		{
			log.getLogNode().add(nodes[i]);
		}
		return log;
	}
	
	public static void success(Session session, LogNode node, String content) throws ClassNotFoundException, UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logSingleNode(node, "[SUCC] "+content, STATUS_SUCCESS);
		session.save(log);
	}
	
	public static void success(Session session, LogNode[] nodes, String content) throws UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logMultiNodes(nodes, "[SUCC] "+content, STATUS_SUCCESS);
		session.save(log);
	}
	
	public static void failure(Session session, LogNode node, String content) throws ClassNotFoundException, UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logSingleNode(node, "[FAIL] "+content, STATUS_FAILURE);
		session.save(log);
	}
	
	public static void failure(Session session, LogNode[] nodes, String content) throws UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logMultiNodes(nodes, "[FAIL] "+content, STATUS_FAILURE);
		session.save(log);
	}
	
	public static void info(Session session, LogNode node, String content) throws ClassNotFoundException, UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logSingleNode(node, "[INFO] "+content, STATUS_INFO);
		session.save(log);
	}
	
	public static void info(Session session, LogNode[] nodes, String content) throws UnsuportedFieldException, DocumentException, Exception
	{
		Log log = logMultiNodes(nodes, "[INFO] "+content, STATUS_INFO);
		session.save(log);
	}
	
	private static Result<Log> getLogResult(Session session, LogNode node, int count) throws SQLException, DocumentException, UnsuportedFieldException, Exception
	{
		Query<Log> query = new Query<Log>(session);
		Result<Log> result = query.find(Log.class)
				.association(Log.class, LogNode.class, "`_id` = " + node.getSpecId(LogNode.class))
				.extend(LogNode.class, node.getClass())
				.order(Log.class, "`_id` DESC")
				.limit(0, count)
				.result();
		return result;
	}

	public static void printLog(Session session, LogNode node, int count) throws SQLException, DocumentException, UnsuportedFieldException, Exception
	{
		Result<Log> result = getLogResult(session, node, count);
		Iterator<Log> iterator = result.iterator();
		while(iterator.hasNext())
		{
			System.out.println(iterator.next().toString()+","+node.toString());
		}
	}
	
	public static Log[] getLog(Session session, LogNode node, int count) throws SQLException, DocumentException, UnsuportedFieldException, Exception
	{
		Result<Log> result = getLogResult(session, node, count);
		Iterator<Log> iterator = result.iterator();
		Log[] logs = new Log[result.count()];
		int index = 0;
		while(iterator.hasNext())
		{
			logs[index++] = iterator.next();
		}
		return logs;
	}

	public String getContent() {
		return content.string;
	}

	public void setContent(String content) {
		this.content.string = content;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public Relation getLogNode() {
		return logNode;
	}

	@Override
	public String toString() {
		return "Log [content=" + content + ", status=" + status + ", logNode=" + logNode + ", _id=" + _id + ", _v=" + _v
				+ "]";
	}
	
	
}
